﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace PeekService.Service
{
    public class ViewLogsService
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ViewLogs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ViewLogs(");
            strSql.Append("Code,CreateDate,State,IpAddress,UserMac,Country,Province,City,Url,LastViewURL,Remark");
            strSql.Append(") values (");
            strSql.Append("@Code,@CreateDate,@State,@IpAddress,@UserMac,@Country,@Province,@City,@Url,@LastViewURL,@Remark");
            strSql.Append(") ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Code", SqlDbType.VarChar,36) ,            
                        new SqlParameter("@CreateDate", SqlDbType.DateTime) ,            
                        new SqlParameter("@State", SqlDbType.Int,4) ,            
                        new SqlParameter("@IpAddress", SqlDbType.Char,20) ,            
                        new SqlParameter("@UserMac", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Country", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Province", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@City", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Url", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@LastViewURL", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Remark", SqlDbType.NVarChar,200)             
              
            };

            parameters[0].Value = model.Code;
            parameters[1].Value = model.CreateDate;
            parameters[2].Value = model.State;
            parameters[3].Value = model.IpAddress;
            parameters[4].Value = model.UserMac;
            parameters[5].Value = model.Country;
            parameters[6].Value = model.Province;
            parameters[7].Value = model.City;
            parameters[8].Value = model.Url;
            parameters[9].Value = model.LastViewURL;
            parameters[10].Value = model.Remark;
            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ViewLogs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ViewLogs set ");

            strSql.Append(" Code = @Code , ");
            strSql.Append(" CreateDate = @CreateDate , ");
            strSql.Append(" State = @State , ");
            strSql.Append(" IpAddress = @IpAddress , ");
            strSql.Append(" UserMac = @UserMac , ");
            strSql.Append(" Country = @Country , ");
            strSql.Append(" Province = @Province , ");
            strSql.Append(" City = @City , ");
            strSql.Append(" Url = @Url , ");
            strSql.Append(" LastViewURL = @LastViewURL , ");
            strSql.Append(" Remark = @Remark  ");
            strSql.Append(" where Code=@Code  ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Code", SqlDbType.VarChar,36) ,            
                        new SqlParameter("@CreateDate", SqlDbType.DateTime) ,            
                        new SqlParameter("@State", SqlDbType.Int,4) ,            
                        new SqlParameter("@IpAddress", SqlDbType.Char,20) ,            
                        new SqlParameter("@UserMac", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@Country", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Province", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@City", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Url", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@LastViewURL", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@Remark", SqlDbType.NVarChar,200)             
              
            };

            parameters[0].Value = model.Code;
            parameters[1].Value = model.CreateDate;
            parameters[2].Value = model.State;
            parameters[3].Value = model.IpAddress;
            parameters[4].Value = model.UserMac;
            parameters[5].Value = model.Country;
            parameters[6].Value = model.Province;
            parameters[7].Value = model.City;
            parameters[8].Value = model.Url;
            parameters[9].Value = model.LastViewURL;
            parameters[10].Value = model.Remark;
            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ViewLogs ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;
            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ViewLogs GetModel(string Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Code, CreateDate, State, IpAddress, UserMac, Country, Province, City, Url, LastViewURL, Remark  ");
            strSql.Append("  from ViewLogs ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;
            ViewLogs model = new ViewLogs();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                if (dr.Read())
                {
                    return DataRowToModel(dr);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取所有分页
        /// </summary>
        /// <returns></returns>
        public List<ViewLogs> GetAll(int pageIndex, int pageSize, out int RecordCount)
        {
            string strsql = @"select   Code,TourismCode,Title,Content,Remark,State,CreateID,CreateTime,Sort from MapInfo order by sort desc";
            List<ViewLogs> list = new List<ViewLogs>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql, pageIndex, pageSize, out RecordCount))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }
        /// <summary>
        /// 获得所有
        /// </summary>
        public List<ViewLogs> GetAll()
        {
            string strsql = "select Code, CreateDate, State, IpAddress, UserMac, Country, Province, City, Url, LastViewURL, Remark FROM ViewLogs ";
            List<ViewLogs> list = new List<ViewLogs>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ViewLogs DataRowToModel(SqlDataReader reader)
        {
            ViewLogs model = new ViewLogs();
            model.Code = reader["Code"].ToString();
            if (reader["CreateDate"].ToString() != "")
            {
                model.CreateDate = DateTime.Parse(reader["CreateDate"].ToString());
            }
            if (reader["State"] != DBNull.Value)
            {
                model.State = int.Parse(reader["State"].ToString());
            }
            if (reader["CreateDate"] != DBNull.Value)
            {
                model.IpAddress = reader["IpAddress"].ToString();
            }
            if (reader["CreateDate"]!= DBNull.Value)
            {
                model.UserMac = reader["UserMac"].ToString();
            } 
            if (reader["CreateDate"]!= DBNull.Value)
            {
                model.Country = reader["Country"].ToString();
            }
            if (reader["CreateDate"]!= DBNull.Value)
            {
                model.Province = reader["Province"].ToString();
            }
            if (reader["CreateDate"]!= DBNull.Value)
            {
                model.City = reader["City"].ToString();
            }
            if (reader["CreateDate"]!= DBNull.Value)
            {
                model.Url = reader["Url"].ToString();
            }
            if (reader["CreateDate"]!= DBNull.Value)
            {
                model.LastViewURL = reader["LastViewURL"].ToString();
            }
            if (reader["CreateDate"]!= DBNull.Value)
            {
                model.Remark = reader["Remark"].ToString();
            }
            return model;
        }
    }
}
